using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityGameFramework.Editor;
using LS.Saving;
using LS.UnityExtensions.Editor;

namespace LS.Editor.Saving
{
    //[CustomEditor(typeof(SavingComponent))]
    public class SavingComponentInspector : InspectorBase
    {

        //private SerializedProperty m_MaxSavingCount;
        private SerializedProperty m_PeriodicSaving = null;
        private SerializedProperty m_AutoSavingTimeInterval = null;
        private SerializedProperty m_SaveOnApplicationFocusOut = null;
        private SerializedProperty m_LoadOnApplicationFocusIn = null;
        private HelperInfo<SaveHelperBase> m_SavingHelperInfo = new HelperInfo<SaveHelperBase>("Saving");

        
        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();

            //SavingComponent t = (SavingComponent)target;

            //EditorGUI.BeginDisabledGroup(EditorApplication.isPlayingOrWillChangePlaymode);
            //{
            //    EditorGUILayout.PropertyField(m_PeriodicSaving);
            //    if (m_PeriodicSaving.boolValue)
            //    {
            //        EditorGUILayout.PropertyField(m_AutoSavingTimeInterval);
            //    }
            //    EditorGUILayout.Space();
            //    EditorGUILayout.PropertyField(m_SaveOnApplicationFocusOut);
            //    EditorGUILayout.PropertyField(m_LoadOnApplicationFocusIn);
            //    m_SavingHelperInfo.Draw();
            //}
            //EditorGUI.EndDisabledGroup();

            //if (EditorApplication.isPlaying && IsPrefabInHierarchy(t.gameObject))
            //{
            //    EditorGUILayout.LabelField("Saving Count", t.Count >= 0 ? t.Count.ToString() : "<Unknown>");
            //    if (t.Count > 0)
            //    {
            //        string[] settingNames = t.GetAllSavingNames();
            //        foreach (string settingName in settingNames)
            //        {
            //            EditorGUILayout.LabelField(settingName, t.GetString(settingName));
            //        }
            //    }
            //}

            //if (EditorApplication.isPlaying)
            //{
            //    if (GUILayout.Button("Save Saving"))
            //    {
            //        t.Save();
            //    }
            //    if (GUILayout.Button("Remove All Savings"))
            //    {
            //        t.RemoveAllSavings();
            //    }
            //}

            //serializedObject.ApplyModifiedProperties();

            //Repaint();
        }

        //protected override void OnCompileComplete()
        //{
        //    base.OnCompileComplete();

        //    RefreshTypeNames();
        //}

        protected override void OnEnable()
        {
            base.OnEnable();
            m_PeriodicSaving = serializedObject.FindProperty("m_PeriodicSaving");
            m_AutoSavingTimeInterval = serializedObject.FindProperty("m_AutoSavingSeconds");
            m_SaveOnApplicationFocusOut = serializedObject.FindProperty("m_SaveOnApplicationFocusOut");
            m_LoadOnApplicationFocusIn = serializedObject.FindProperty("m_LoadOnApplicationFocusIn");
            m_SavingHelperInfo.Init(serializedObject);

            RefreshTypeNames();
        }

        private void RefreshTypeNames()
        {
            m_SavingHelperInfo.Refresh();
            serializedObject.ApplyModifiedProperties();
        }
    }
}
